package tcp;


import java.io.*;
/*
        TCP协议的三次握手
 */
import java.net.Socket;
import java.util.Scanner;

public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建Socket对象
        Socket socket = new Socket("127.0.0.1",10086);//主机IP地址  端口号
            //通过Socket对象获取网络中的字节输出流来发数据
            OutputStream os = socket.getOutputStream();
            //利用OutputStreamWriter来将字节流转换，防止发送的信息中有中文，服务器从而无法解码导致乱码
            OutputStreamWriter osw = new OutputStreamWriter(os);
            Scanner sc = new Scanner(System.in);
            String details = sc.next();
            osw.write(details);
            osw.flush();
            //给服务器一个结束标记
            socket.shutdownOutput();

            //接收服务器端传来的数据
            //将网络中的字节输入流封装为高效的字符输入流
            BufferedReader bis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while((line=bis.readLine())!=null) {
                System.out.println(line);
            }
            //释放资源
            socket.close();
    }
}
